<?php
class SeminaController extends AppController{
	var $name = 'Semina';
	var $uses = array (
		'SysParams',
		'Member',
		'MailJob',
		'Semina',
		'SeminaOrder'
	);
	
	function ajax_city(){
		$this->layout='ajax';
		$p=$this->data['Member']['address_nation_id'];
		$condition=" kbn ='city_id' and pid=".$p;
		$city_list=$this->SysParams->generateList($condition);
		$this->set("city_list",$city_list);
	}
	
	function birthday_yy(){
		$this->layout='ajax';
		$y=$this->data['Member']['bir_year'];
		$m=$this->Session->read("mm");
		$daylist=array();
		if($m==null){
			$this->Session->write('yyyy',$y);
		}else if($m!=null){	
			if($m==1||$m==3||$m==5||$m==7||$m==8||$m==10||$m==12){
				for($i=1;$i<=31;$i++){
					$daylist[$i]=$i;
				}
			}
			if($m==4||$m==6||$m==9||$m==11){
				for($i=1;$i<=30;$i++){
					$daylist[$i]=$i;
				}	
			}
			if($y%4==0&&$m==2){
				$index=0;
				for($i=1;$i<=29;$i++){
					$daylist[$i]=$i;
				}
			}else if($y%4!=0&&$m==2){
				for($i=1;$i<=28;$i++){
					$daylist[$i]=$i;
				}
			}
		}
		$this->set("daylist",$daylist);
	}
	
	function birthday_mm(){
		$this->layout='ajax';
		$y=$this->Session->read('yyyy');
		$m=$this->data['Member']['bir_month'];
		$daylist=array();
		if($y!=null&&$y!=""){
			if($m==1||$m==3||$m==5||$m==7||$m==8||$m==10||$m==12){
				for($i=1;$i<=31;$i++){
					$daylist[$i]=$i;
				}
			}
			if($m==4||$m==6||$m==9||$m==11){
				for($i=1;$i<=30;$i++){
					$daylist[$i]=$i;
				}
			}
			if($y%4==0&&$m==2){
				$index=0;
				for($i=1;$i<=29;$i++){
					$daylist[$i]=$i;
				}
			}else if($y%4!=0&&$m==2){
				for($i=1;$i<=28;$i++){
					$daylist[$i]=$i;
				}
			}
		}else if($y==null){
			$this->Session->write("mm",$m);	
		}
		$this->set("daylist",$daylist);
	}
	
	function myInit(){
		$country_list2=$this->SysParams->generateList("kbn = 'country_id' and deleted is null");
		$this->set("country_list2",$country_list2);
		$country_list=$this->SysParams->generateList(" kbn='nationality_id' and deleted is null",'sort');
		$this->set("country_list",$country_list);
		$condition2=" kbn ='city_id' ";
		$city_list=$this->SysParams->generateList($condition2);
		
		$this->set("city_list",$city_list);
			
		$condition3="kbn = 'interest_work_type_id' and deleted is null";
		$work_list=$this->SysParams->generateList($condition3);
		$this->set("work_list",$work_list);
		$condition4="kbn = 'education_id' and deleted is null";
		$education_list=$this->SysParams->generateList($condition4);
		$this->set("education_list",$education_list);
		$condition5="kbn='work_status_id' and deleted is null";
		$work_status=$this->SysParams->generateList($condition5);
		$this->set("work_status",$work_status);
		$condition6="kbn='sex_id' and deleted is null";
		$sex_list=$this->SysParams->generateList($condition6);
		$this->set("sex_list",$sex_list);
		$condition7="kbn='language_id' and deleted is null ";
		$language_list=$this->SysParams->generateList($condition7);
		$this->set("language_list",$language_list);
		$condition8="kbn='password_question_id' and deleted is null";
		$pwd_list=$this->SysParams->generateList($condition8);
		$this->set("pwd_list",$pwd_list);
		$y=date('Y');
		$yearlist=array();
		
		for($i=$y-76;$i<=$y-16;$i++)
		{
			$yearlist[$i]=$i;
		
		}
		$this->set("yearlist",$yearlist);
		$monthlist=array();
	
		for($i=1;$i<=12;$i++)
		{
			$monthlist[$i]=$i;
			
		}
		$this->set("monthlist",$monthlist);
		$daylist=array();
		
		for($i=1;$i<=31;$i++)
		{
			$daylist[$i]=$i;
			
		}
		$this->set("daylist",$daylist);
		
	}
	
	function form_init($path){
		$this->Session->del('path');
		
		$ini_arr = parse_ini_file("../views/".$path."/config.ini", true);
		//pr($ini_arr);
		$ini_array = array_splice($ini_arr,0,-1);
		$this->Session->write('email_arr',$ini_arr);
		$this->Session->write('origin_arr',$ini_array);
		$this->Session->write('path',$path);
		foreach($ini_array as $index=>&$ele){
			
			if($ele['type']=='textbox'){
				$ele['ele']='<input type="text" name="data[Member]['.$index.']" class="i"  value="'.$this->data['Member'][$index].'"/>';
			}
			if($ele['type']=='textarea'){
				$ele['ele']='<textarea name="data[Member]['.$index.']" class="ta">'.$this->data['Member'][$index].'</textarea>';
			}		
			if($ele['type']=='select'){		
			    $sele='<select type="select" name="data[Member]['.$index.']" class="s2">';
				if(!empty($ele['value'])){
					$value=explode(',',$ele['value']);
					foreach($value as $in=>$va){
						if(!empty($this->data)&&isset($this->data['Member'][$index])&&$this->data['Member'][$index]==$in){
							$sele.='<option value="'.$in.'" selected>'.$va.'</option>';
						}else{
							$sele.='<option value="'.$in.'">'.$va.'</option>';
						}
					}
				}
				$sele.='</select>';
				$ele['ele']=$sele;
			}
			if($ele['type']=='radio'){
				$value=explode(',',$ele['value']);
				$rad='';
				foreach($value as $in=>$val){
					if(!empty($val)){
						if(!empty($this->data)&&isset($this->data['Member'][$index])&&$this->data['Member'][$index]==$in){
							$rad.='&nbsp;&nbsp;<input type="radio" name="data[Member]['.$index.']" value="'.$in.'" checked>'.$val.'</input>';
						}else{
							$rad.='&nbsp;&nbsp;<input type="radio" name="data[Member]['.$index.']" value="'.$in.'">'.$val.'</input>';
						}
					}
				}
				$ele['ele']=$rad;
			}
		}	
		
		$this->set('path',$path);
		$this->set('ini_arr',$ini_array);
// 		print_r($ini_arr);
	}
	
	function beforlogin () {
		
	}
	
	function form($path='semina') {
		$this->form_init($path);
		$this->myInit();
	    if(empty($this->data)){	
			if($this->rdAuth->id){
				$member=$this->Member->find("Member.id=".$this->rdAuth->id);
				if(!empty($member)){
					$member['Bird']=explode('-',$member['Member']['birthday']);
					$this->Session->write('yyyy',$member['Bird'][0]);
				    $this->Session->write('mm',$member['Bird'][1]);
					$this->set('member',$member);
					$this->data=$member;
					$this->data['Member']['bir_year']=$member['Bird'][0];
			    	$this->data['Member']['bir_month']=$member['Bird'][1];
			    	$this->data['Member']['bir_day']=$member['Bird'][2];
		    	}
			}

			$ini_array = array_splice(parse_ini_file("../views/".$path."/config.ini", true),0,-1);
			if (!empty($ini_array['semina_id']) && empty($this->rdAuth->id)) {
				$this->redirect('/semina/beforlogin/');
				exit;
			}			
			
	    	$this->render('../'.$path.'/form');
	    }else{
	    	$message=$this->valid_form($this->Valid,$this->data);
	    	if(!empty($message)){
	    		$this->set('message',$message);
	    		$this->render('../'.$path.'/form');
	    		exit;
	    	}
	    	$this->Session->write('form',$this->data);
	    	$this->redirect('/semina/confirm');
	    }
	}
	
	function confirm() {
	    $path=$this->Session->read('path');
	    $form=$this->Session->read('form');
	    $this->Output->htmlspecialchars($form);	    
		$ini_array=$this->Session->read('origin_arr');
		foreach($ini_array as $index=>&$ele){
			
			//@author baimengzhao to confirm add start 2012/4/5
			if($ele['type']=='hidden'){
				$ele['ele']=$form['Member']['semina_id'];
			}
			//@author baimengzhao to confirm add end 2012/4/5
			
			if($ele['type']=='kanji_ssi'){
				$ele['ele']=$form['Member']['name_kanji'];
			}
			
			if($ele['type']=='kana_ssi'){
				$ele['ele']=$form['Member']['name_kana'];
			}
			
			if($ele['type']=='sex_ssi'){
				if($form['Member']['sex_id']==1){
					$ele['ele']='男';
				}elseif($form['Member']['sex_id']==2){
					$ele['ele']='女';
				}
			}

			if($ele['type']=='birthday_ssi'){
				$ele['ele']=$form['Member']['bir_year'].'-'.$form['Member']['bir_month'].'-'.$form['Member']['bir_day'];
			}

			if($ele['type']=='address_ssi'&&(!empty($form['Member']['address_nation_id'])||!empty($form['Member']['address_location_id'])||!empty($form['Member']['address_location_other']))){
				$nation=$this->SysParams->find("\"SysParams\".\"kb\"n='country_id' and \"SysParams\".\"id\"=".$form['Member']['address_nation_id']);
				if(isset($form['Member']['address_location_id'])&&!empty($form['Member']['address_location_id'])){
					$city=$this->SysParams->find("\"SysParams\".\"kbn\"='city_id' and \"SysParams\".\"id\"=".$form['Member']['address_location_id']);
				}
				$ele['ele']=$nation['SysParams']['name'];
				if(isset($city)){
					$ele['ele'].=$city['SysParams']['name'];
				}
				if($form['Member']['address_nation_id']==3&&$form['Member']['address_location_id']==92&&!empty($form['Member']['address_location_other'])){
					$ele['ele'].=$form['Member']['address_location_other'];
				}
			}

			if($ele['type']=='nation_ssi'&&(!empty($form['Member']['nationality_id']))){
				$nationality=$this->SysParams->find("\"SysParams\".\"kbn\"='nationality_id' and \"SysParams\".\"id\"=".$form['Member']['nationality_id']);
				$ele['ele']=$nationality['SysParams']['name'];
			}

			if($ele['type']=='email_ssi'){
				$ele['ele']=$form['Member']['email'];
			}

			if($ele['type']=='free'){
				$ele['label']=$form['Member'][$index.'_label'];
				$ele['ele']=$form['Member'][$index];
			}

			if($ele['type']=='radio'&&isset($form['Member'][$index])){			
				$value=explode(',',$ele['value']);			
				$ele['ele']=$value[$form['Member'][$index]];
			}

			if($ele['type']=='select'&&isset($form['Member'][$index])){
				$value=explode(',',$ele['value']);	
				$ele['ele']=$value[$form['Member'][$index]];
			}

			if($ele['type']=='language_ssi'){
				$lang='<table cellpadding="0" cellspacing="0" border="0">';
				$lang2='';
				if($form['Member']['language_jp_id']!=0){
					$lang_jp=$this->SysParams->find("\"SysParams\".\"kbn\"='language_id' and \"SysParams\".\"id\"=".$form['Member']['language_jp_id']);
					$lang.='<tr><td>日本語</td><td width="120">'.$lang_jp['SysParams']['name'].'</td></tr>';
					$lang2.='日本語:'.$lang_jp['SysParams']['name'].';';
				}
				if($form['Member']['language_ch_bj_id']!=0){
					$lang_cn=$this->SysParams->find("\"SysParams\".\"kbn\"='language_id' and \"SysParams\".\"id\"=".$form['Member']['language_ch_bj_id']);
					$lang.='<tr><td nowrap="nowrap" width="88">中国語（普通語）</td><td>'.$lang_cn['SysParams']['name'].'</td></tr>';
					$lang2.='中国語（普通語）:'.$lang_cn['SysParams']['name'].';';
				}
				if($form['Member']['language_en_id']!=0){
					$lang_en=$this->SysParams->find("\"SysParams\".\"kbn\"='language_id' and \"SysParams\".\"id\"=".$form['Member']['language_en_id']);
					$lang.=' <tr><td>英語</td><td>'.$lang_en['SysParams']['name'].'</td></tr>';
					$lang2.='英語:'.$lang_en['SysParams']['name'].';';
				}
				if($form['Member']['language_ch_gd_id']!=0){
					$lang_cn_gd=$this->SysParams->find("\"SysParams\".\"kbn\"='language_id' and \"SysParams\".\"id\"=".$form['Member']['language_ch_gd_id']);
					$lang.=' <tr><td>広東語</td><td>'.$lang_cn_gd['SysParams']['name'].'</td></tr>';
					$lang2.='広東語:'.$lang_cn_gd['SysParams']['name'].';';
				}
				if($form['Member']['language_other_id1']!=0){
					$lang_other1=$this->SysParams->find("\"SysParams\".\"kbn\"='language_id' and \"SysParams\".\"id\"=".$form['Member']['language_other_id1']);
					$lang.=' <tr><td>'.$form['Member']['language_other_note1'].'</td><td>'.$lang_other1['SysParams']['name'].'</td></tr>';
					$lang2.=$form['Member']['language_other_note1'].':'.$lang_other1['SysParams']['name'].';';
				}
				if($form['Member']['language_other_id2']!=0){
					$lang_other2=$this->SysParams->find("\"SysParams\".\"kbn\"='language_id' and \"SysParams\".\"id\"=".$form['Member']['language_other_id2']);
					$lang.=' <tr><td>'.$form['Member']['language_other_note2'].'</td><td>'.$lang_other2['SysParams']['name'].'</td></tr>';
					$lang2.=$form['Member']['language_other_note2'].':'.$lang_other2['SysParams']['name'].';';
				}
				$ele['ele']=$lang.'</table>';
				$ele['ele2']=$lang2;
			}

			if($ele['type']=='textbox'){
				$ele['ele']=$form['Member'][$index];
			}

			if($ele['type']=='textarea'){
				$ele['ele']=$form['Member'][$index];
			}
			
			//@author baimengzhao to confirm add start 2012/4/5
			if($ele['type'] != 'hidden'){
				$semina_order .= $ele['label'].'='.$ele['ele'].'<br>';
			}
			//@author baimengzhao to confirm add end 2012/4/5
		}
		$this->Session->write('semina_order',$semina_order);
		
		if(!empty($form['Member']['semina_id'])) {
			$this->Session->write('semina_id', $form['Member']['semina_id']);
		} else {
			if($this->Session->check('semina_id')) {
				$this->Session->del('semina_id');
			}
		}

		$this->Session->write('ini_arr',$ini_array);
	    unset($ini_array['semina_id']);
		$this->set('ini_arr',$ini_array);
	    $this->data=$form;
	    $this->render('../'.$path.'/confirm');
	}	
	
	//@author baimengzhao to add_order add start 2012/4/5
	function add_order()
	{
		$order_info = Array();
		
	    $id = $this->rdAuth->id;
	    if(!empty($id)){
	    	$order_info['member_id'] = $id;
	    }
		$order_content = $this->Session->read('semina_order');
		if(!empty($order_content)){
			$order_info['order_content'] = $order_content;
			$order_info['semina_id'] = $this->Session->read('semina_id');
			$this->Session->del('semina_order');
			$this->Session->del('semina_id');
		}
	
		$order_info['status'] = 0;
		
		$this->SeminaOrder->save($order_info);
		
		unset($order_info);
		
		
		
	}
	
	function email_send() {
		
		if (!$this->Session->check('ini_arr') || !$this->Session->check('email_arr') ) {
			$this->redirect('semina/finish');
			exit;
		} 
		
		$arr=$this->Session->read('ini_arr');
		$email=$this->Session->read('email_arr');
		
		if ($this->Session->check('semina_id')) {
			$this->add_order();
			
			$contents=str_replace('##contents##',
					'ご応募の内容については、MyPage(http://localhost:82/memberLogin/login)よりログインしてご確認頂けます。',
					$email['Mail_config']['contents']);
			
			$contents1=str_replace('##contents##',
					'http://' . ADMIN_SERVER_NAME."/company/apply_detail/" . $this->SeminaOrder->id,
					$email['Mail_config']['contents']);

			$this->MailJob->saveMsg($email['Mail_config']['from'],$email['Mail_config']['to'],$email['Mail_config']['subject'],$contents1);
			
			if(isset($arr['email']['ele'])&&!empty($arr['email']['ele'])){
				$this->MailJob->saveMsg($email['Mail_config']['from'],$arr['email']['ele'],$email['Mail_config']['subject'],$contents);
			}
			$this->Session->del('semina_id');
			
		} else {
	    	$cont=$this->getMailTemplate("form_finish",$arr);
	    	$contents=str_replace('##contents##',$cont, $email['Mail_config']['contents']);
	    	
	    	$this->MailJob->saveMsg($email['Mail_config']['from'],$email['Mail_config']['to'],$email['Mail_config']['subject'],$contents);
	    	if(isset($arr['email']['ele'])&&!empty($arr['email']['ele'])){
	    		$this->MailJob->saveMsg($email['Mail_config']['from'],$arr['email']['ele'],$email['Mail_config']['subject'],$contents);
	    	}
		}

		
// 		$this->MailJob->saveMsg($email['Mail_config']['from'],$email['Mail_config']['to'],$email['Mail_config']['subject'],$contents1);
		
// 		if(isset($arr['email']['ele'])&&!empty($arr['email']['ele'])){
// 			$this->MailJob->saveMsg($email['Mail_config']['from'],$arr['email']['ele'],$email['Mail_config']['subject'],$contents);
// 		}
		
	    $this->Session->del('ini_arr');
		$this->Session->del('email_arr');
		
		$this->redirect('semina/finish');
		
	}
	
	function finish(){
		$path=$this->Session->read('path');
		$this->render('../'.$path.'/finish');	
	}
	
	function valid_form($Valid,$params){	
		$ini_array=$this->Session->read('origin_arr');
// 		print_r($ini_array);
		$message='';
		foreach($ini_array as $index=>$ele){
			if(isset($ele['valid_required'])&&$ele['valid_required']==1){
				if($ele['type']=='birthday_ssi'){
					if(empty($params['Member']['bir_year'])||empty($params['Member']['bir_month'])||empty($params['Member']['bir_day']))
					$message .= $this->Valid->tagErrorMsg('required',$ele['label']);
				}elseif($ele['type']=='address_ssi'){
					$condition1=empty($params['Member']['address_nation_id'])||empty($params['Member']['address_location_id']);
					$condition2=$params['Member']['address_nation_id']==3&&empty($params['Member']['address_location_other']);
					if($condition1||$condition2){
						$message .= $this->Valid->tagErrorMsg('required',$ele['label']);
					}
				}else{
					if(!$Valid->required($params['Member'][$index])){
						$message.=$Valid->tagErrorMsg('required',$ele['label']);
                	}
				}
			}
			if(isset($ele['valid_maxlength'])&&!empty($ele['valid_maxlength'])){
				if(!$Valid->maxlength($params['Member'][$index],$ele['valid_maxlength'])){
					$message.=$Valid->tagErrorMsg('maxlength',$ele['label'],$ele['valid_maxlength']);
                }
			}
			if(isset($ele['valid_email'])&&$ele['valid_email']==1){
				if(!$Valid->email($params['Member'][$index])){
					$message.=$Valid->tagErrorMsg('email',$ele['label']);
                }	
			}
			if(isset($ele['valid_number'])&&$ele['valid_number']==1){
                if(!$Valid->number($params['Member'][$index])){
					$message.=$Valid->tagErrorMsg('number',$ele['label']);
                }	
			}
			if(isset($ele['valid_url'])&&$ele['valid_url']==1){
				if(!$Valid->url($params['Member'][$index])){
					$message.=$Valid->tagErrorMsg('url',$ele['label']);
                }
			}
			if(isset($ele['valid_regex'])&&!empty($ele['valid_regex'])){
				if(!$Valid->regex($params['Member'][$index],$ele['valid_regex'])){
					$mess=str_replace('{0}',$ele['label'], $ele['valid_regex_message']);
					$message.=$Valid->tagErrorMsg($mess);
                }
			}
		}
		
// 		if(!isset($ini_array['semina_id']['type']) ||(isset($ini_array['semina_id']['type']) && empty($ini_array['semina_id']['value'])))
// 		{
// 			$message.=$Valid->tagErrorMsg('semina_id');
// 		}
		return $message;
	}
}
?>
